<?php
/* 
 * Copyright (c) 2018-2021   All rights reserved.
 * 
 * 创建时间：2021-08-28 14:26
 *
 * 项目：rm  -  $  - index.php
 *
 * 作者：liwei 
 */

use modules\levpays\sdk\wechatpay\h5\H5;
use modules\levpays\sdk\wechatpay\InstancePay;
use modules\levpays\sdk\wechatpay\jsapi\Jsapi;
use modules\levpays\sdk\wechatpay\native\Native;

?>

<div class="page pay-index">
    <?php Lev::navbar(); Lev::toolbar(); ?>
    <div class="page-content" style="background: #F3F3F3">
        <div class="page-content-inner" style="max-width: 660px;background:#ffffff;min-height:100%">
            <div class="flex-box ju-sas appbg" style="height:150px;position: relative">
                <amount class="scale9" style="font-size:48px;font-family: '黑体', fantasy;font-weight: 800;color: gold;display: flex;"><?=$paymoney?></amount>
                <span class="scale8" style="font-size: 14px;display: flex;color: #dddddd;font-weight: 700;margin-top: 20px;"><?=$unit?></span>
                <div class="levpays-qrcode-box hiddenx" style="position: absolute;top:1px;right:1px;">
                    <div id="levpays-qrcode"></div>
                    <tips style="position: absolute;top: 68px;text-align: center;display: block;background: rgba(255,255,255, 0.9);right: 40px;padding: 0 5px;border-radius: 3px;text-shadow: 0 0 5px yellow;">扫一扫支付</tips>
                </div>
            </div>

            <div class="card-header">支付方式</div>
            <div class="list-block no-hairlines" style="margin-top: 0">
                <ul>
                    <?php foreach ($paytypeids as $k => $v):?>
                    <li class="paytypeidbox-<?=$k?>">
                        <label class="label-radio item-content">
                            <input type="radio" name="paytypeid" value="<?=$k?>" <?=$paytypeid==$k ? 'checked="checked"' : ''?>>
                            <div class="item-media icon-img">
                                <?=Lev::arrv($k, $paytypeidIcons)?>
                            </div>
                            <div class="item-inner">
                                <div class="item-title"><?=$v?></div>
                            </div>
                        </label>
                    </li>
                    <?php endforeach;?>
                </ul>
            </div>
            <div class="card-header">
                <span>商品信息</span>
                <tips>订单号：<?=$goods['tradeNo']?></tips>
            </div>
            <div class="list-block no-hairlines" style="margin-top: 0">
                <ul>
                <?php if (!empty($goods['lists'])):foreach ($goods['lists'] as $gv):?>
                    <li>
                        <label class="item-content">
                            <div class="item-media icon-img" style="width: 40px;height: 40px;font-size: 40px;padding:0">
                                <?=$gv['img']?>
                            </div>
                            <div class="item-inner">
                                <div class="item-title font14"><?=$gv['name']?></div>
                                <div class="item-after"><b class="red font14"><?=$gv['price']?></b></div>
                            </div>
                        </label>
                    </li>
                <?php endforeach;else:?>
                    <li>
                        <label class="item-content">
                            <div class="item-media icon-img" style="width: 40px;height: 40px;font-size: 40px;padding:0">
                                <?=$goods['img']?>
                            </div>
                            <div class="item-inner">
                                <div class="item-title font14"><?=$goods['name']?></div>
                            </div>
                        </label>
                    </li>
                <?php endif;?>
                    <li>
                        <label class="item-content">
                            <div class="item-inner">
                                <div class="item-title font12 color-gray"><?=$goods['descs']?></div>
                            </div>
                        </label>
                    </li>
                </ul>
            </div>

            <div class="flex-box ju-sa">
                <div class="button-fill button color-green levpaysPaySubmitBtn" style="width:calc(100% - 20px);font-size:16px;margin:0 auto 25px">确定支付</div>
            </div>

            <div class="loadingPay flex-box ju-sa color-gray"></div>

        </div>
    </div>

<div class="LoadPageAjaxJS checkPayTradeNoBox">

<script>
var levpaysPay = {};

jQuery(function () {
    levpaysPay = {
        pre: '',
        checkPayTradeNo: 1,
        levpaysPaySubmit: function () {
        },
        levpaysAlipaySubmit: function () {
        },
        changePaytypeid: function (paytypeid) {
            paytypeid = parseInt(paytypeid === undefined ? levpaysPay.getPaytypeid() : paytypeid);

            jQuery('.levpaysPaySubmitBtn').removeClass('disabled').html('确定支付');
            typeof levpaysPay.setPaytypeidInit[paytypeid] === "function" && levpaysPay.setPaytypeidInit[paytypeid]();

            !levpaysPay.checkPayTradeNo && levpaysPay.init();
        },
        setPaytypeidInit: {},
        setPaytypeidSubmit: {},
        paytypeInput: function (obj) {
            var cls = 'input[name=paytypeid]';
            if (obj === 2) return cls;
            cls = Levme.conf.showPageContainer + ' ' + cls;
            return obj ? jQuery(cls) : cls;
        },
        getPaytypeid: function () {
            var inputcls = levpaysPay.paytypeInput();
            var paytypeid = parseInt(jQuery(inputcls + ':checked').val());
            if (isNaN(paytypeid)) {
                paytypeid = parseInt(jQuery(inputcls).eq(0).val());
                jQuery(inputcls).eq(0).attr('checked', true);
            }
            return paytypeid;
        },
        init: function (checkPayTradeNoOpen) {
            window.setTimeout(function () {
                levpaysPay.checkPayTradeNo = 1;
                checkPayTradeNo();
            }, 1000);

            if (checkPayTradeNoOpen) return;

            function payedTip() {
                myApp.confirm('我已成功支付？', function () {
                    showIconLoader(true);
                    checkTradeNo(undefined, 1);
                }, function () {
                    window.history.back();
                });
            }

            var loadNum = 0;

            function checkPayTradeNo() {
                if (loadNum > 15) {
                    payedTip();
                    return;
                }
                checkTradeNo(function (data, status) {
                    if (status > 0) {
                        jQuery('.checkPayTradeNoTimeout').length > 0 &&
                        window.setTimeout(function () {
                            loadNum += 1;
                            checkPayTradeNo();
                            !(loadNum % 10) ? jQuery('.loadingPay').html('.') :
                                jQuery('.loadingPay').append('.');
                        }, 5000);
                    } else {
                        levpaysPay.checkPayTradeNo = 0;
                        levtoast(data.message, 10000)
                    }
                });
            }

            function checkTradeNo(func, userck) {
                Levme.ajaxv.getv(levToRoute('pay/check-trade-no', {
                    paytypeid: levpaysPay.getPaytypeid(),
                    id: 'levpays', pre: levpaysPay.pre,
                    tradeNo: '<?=$goods['tradeNo']?>',
                    userck: userck,
                }), function (data, status) {//myApp.alert(JSON.stringify(data));
                    data.location && (window.location = data.location === 1 ? '<?=Lev::$app['referer']?>' : data.location);
                    typeof func === "function" && func(data, status);
                    if (userck) {
                        var msg = data && data.message ? data.message : '抱歉，未找到已支付订单';
                        myApp.confirm('是否继续支付？', msg, function () {
                            window.location.reload();
                        }, function () {
                            window.history.back();
                        });
                    }
                });
            }
        }
    };

    Levme.onClick('.levpaysPaySubmitBtn', function () {
        showIconLoader(true);
        var paytypeid = levpaysPay.getPaytypeid();
        if (typeof levpaysPay.setPaytypeidSubmit[paytypeid] === "function") {
            levpaysPay.setPaytypeidSubmit[paytypeid]();
        }else {
            paytypeid === 1 ? levpaysPay.levpaysAlipaySubmit() : levpaysPay.levpaysPaySubmit();
        }
    });

    jQuery('.checkPayTradeNoBox').addClass('checkPayTradeNoTimeout');

    levpaysPay.init();

    jQuery(document).on('change', levpaysPay.paytypeInput(), function () {
        levpaysPay.changePaytypeid();
    });
});

</script>

    <?=\modules\levpays\widgets\pay\payWidget::run($goods)?>

    <script>jQuery(function (){ window.setTimeout(function () { levpaysPay.changePaytypeid(); }, 101); });</script>
</div>
<?php echo Lev::getNotices()?>
</div>